package com.bobi.tfca.ui.mainnavigation.home.activity

import android.annotation.SuppressLint
import android.app.AlertDialog
import android.os.Bundle
import android.view.View
import android.widget.Toast
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.bobi.tfca.base.BaseFragment
import com.bobi.tfca.constants.PageElementIdConstant
import com.bobi.tfca.databinding.FragmentManageActMemberBinding
import com.bobi.tfca.network.client.AppCache
import com.bobi.tfca.network.response.MemberResponse
import com.bobi.tfca.ui.adapter.ActivityMemberAdapter
import com.bobi.tfca.util.LogUtil

class ManageActMemberFragment : BaseFragment<FragmentManageActMemberBinding, ActivityDetailsFragmentVM>(
    FragmentManageActMemberBinding::inflate,
    ActivityDetailsFragmentVM::class.java,
    true
) {
    private lateinit var memberRV: RecyclerView
    private var isOrganizer = false

    @SuppressLint("ResourceType", "UseRequireInsteadOfGet")
    override fun initFragment(
        binding: FragmentManageActMemberBinding,
        viewModel: ActivityDetailsFragmentVM?,
        savedInstanceState: Bundle?
    ) {

        // 设置移除按钮的可见性
        val memberId = AppCache.getMyInfo().memberId
        if (memberId == publicViewModel!!.activity.value!!.organizerId) {
            binding.btRemove.visibility = View.VISIBLE
            isOrganizer = true
        }

        // 初始化RV
        initRecyclerView(binding)

        viewModel!!.apply {
            members.observe(viewLifecycleOwner){
                val adapter = memberRV.adapter as ActivityMemberAdapter
                adapter.memberResponseList = it
                adapter.notifyDataSetChanged()
            }
        }


        // 设置移除监听
        binding.btRemove.setOnClickListener {
            // 获得所有选择会员id
            val adapter = memberRV.adapter as ActivityMemberAdapter
            val memberIds = adapter.getCheckedMemberList()
            if (memberId == null) {
                LogUtil.e("未登录，AppCache为空")
            } else {
                if (memberIds == null || memberIds.size <= 0) {
                    Toast.makeText(requireActivity(), "请至少选择一项", Toast.LENGTH_SHORT).show()
                } else if  (memberId in memberIds) {
                    Toast.makeText(requireActivity(), "不能移除自己", Toast.LENGTH_SHORT).show()
                } else {
                    // 创建确认框
                    val builder = AlertDialog.Builder(requireContext())
                    builder.setTitle("移除")
                    builder.setMessage("你确认移除成员吗")

                    // 设置确认按钮和点击事件
                    builder.setPositiveButton("确认") { dialog, which ->
                        viewModel.removeActivityMember(publicViewModel, this, memberIds, adapter)
                    }

                    // 设置取消按钮和点击事件
                    builder.setNegativeButton("取消") { dialog, which ->
                    }

                    // 创建并显示确认框
                    val dialog: AlertDialog = builder.create()
                    dialog.show()
                }
            }
        }
    }


    /**
     * 初始化RV
     */
    private fun initRecyclerView(
        binding: FragmentManageActMemberBinding
    ) {
        // 获取聊天室RecyclerView实例
        memberRV = binding.rvMember

        // 获得所有活动
        val memberList : ArrayList<MemberResponse> = arrayListOf()
        // 设置布局管理器
        val linearLayoutManager = LinearLayoutManager(binding.root.context)
        linearLayoutManager.orientation = LinearLayoutManager.VERTICAL
        memberRV.layoutManager = linearLayoutManager
        memberRV.addItemDecoration(
            DividerItemDecoration(
                binding.root.context,
                LinearLayoutManager.VERTICAL
            )
        )
        var articleAdapter = ActivityMemberAdapter(this, memberList)
        articleAdapter.isOrganizer = isOrganizer

        // 绑定Adapter
        memberRV.adapter = articleAdapter
    }

    override fun resumeFragment(viewModel: ActivityDetailsFragmentVM?) {
        super.resumeFragment(viewModel)
        // 请求所有活动会员
        viewModel!!.findAllActivityMembers(publicViewModel!!, this)
    }
}